package com.icow.wms.basic.controller;

import java.util.List;

import lombok.extern.slf4j.Slf4j;
import org.springframework.cache.Cache;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.icow.common.annotation.Log;
import com.icow.common.core.controller.BaseController;
import com.icow.common.core.domain.AjaxResult;
import com.icow.common.enums.BusinessType;
import com.icow.wms.basic.domain.LocationMap;
import com.icow.wms.basic.service.ILocationMapService;
import com.icow.common.utils.poi.ExcelUtil;
import com.icow.common.core.page.TableDataInfo;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;

/**
 * locController
 *
 * @author bcs
 * @date 2024-08-20
 */
@Slf4j
@RestController
@RequestMapping("/loc/loc_map")
public class LocationMapController extends BaseController {
    @Autowired
    private ILocationMapService locationMapService;

    @Resource(name = "locMapCache")
    private Cache locMapCache;


    @PostConstruct
    public void init() {
        List<LocationMap> locationMaps = locationMapService.selectAllLocationMap();
        if (locationMaps != null && locationMaps.size() > 0) {
            locationMaps.forEach(loc -> {
                locMapCache.put(loc.getOuterLocation(), loc);
            });
            log.info("locMapCache init success");
        }
    }

    /**
     * 查询loc列表
     */
    @GetMapping("/list")
    public TableDataInfo list(LocationMap locationMap) {
        startPage();
        List<LocationMap> list = locationMapService.selectLocationMapList(locationMap);
        return getDataTable(list);
    }

    /**
     * 导出loc列表
     */
    @Log(title = "loc", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(LocationMap locationMap) {
        List<LocationMap> list = locationMapService.selectLocationMapList(locationMap);
        ExcelUtil<LocationMap> util = new ExcelUtil<LocationMap>(LocationMap.class);
        return util.exportExcel(list, "loc_map");
    }

    /**
     * 获取loc详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(locationMapService.selectLocationMapById(id));
    }

    /**
     * 新增loc
     */
    @Log(title = "loc", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody LocationMap locationMap) {
        int insertRow = locationMapService.insertLocationMap(locationMap);
        locMapCache.put(locationMap.getOuterLocation(), locationMap);
        return toAjax(insertRow);
    }

    /**
     * 修改loc
     */
    @Log(title = "loc", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody LocationMap locationMap) {
        int updateRows = locationMapService.updateLocationMap(locationMap);
        locMapCache.clear();
        List<LocationMap> locationMaps = locationMapService.selectAllLocationMap();
        if (locationMaps != null && locationMaps.size() > 0) {
            locationMaps.forEach(loc -> {
                locMapCache.put(loc.getOuterLocation(), loc);
            });
            log.info("locMapCache update success");
        }
        return toAjax(updateRows);
    }

    /**
     * 删除loc
     */
    @Log(title = "loc", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        int deleteRows = locationMapService.deleteLocationMapByIds(ids);
        locMapCache.clear();
        List<LocationMap> locationMaps = locationMapService.selectAllLocationMap();
        if (locationMaps != null && locationMaps.size() > 0) {
            locationMaps.forEach(loc -> {
                locMapCache.put(loc.getOuterLocation(), loc);
            });
            log.info("locMapCache reInit success");
        }
        return toAjax(deleteRows);
    }
}